<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class DiscountVideoUser extends Model
{
    public $table = 'discount_video_users';

    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s'
    ];

    protected $appends = ['all_video_call_time'];

    const FLAG_OK = 1;
    const FLAG_NO = 0;

    public function userProfile()
    {
        return $this->belongsTo(UserProfile::class, 'user_id', 'user_id');
    }

    public function userScore()
    {
        return $this->belongsTo(UserScore::class, 'user_id', 'user_id');
    }

    public static function getLog(int $userId)
    {
        return self::where('user_id', $userId)->first();
    }

    public function getAllVideoCallTimeAttribute()
    {
        $userId = $this->user_id;

        $userTotal = VideoCall::getTotalCallMinutes($userId, 0);
        $chatUserTotal = VideoCall::getTotalCallMinutes(0, $userId);

        return $userTotal + $chatUserTotal;
    }
}
